package cn.whale.task.config;

import cn.whale.task.mq.fallback.MsgConfirmCallback;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static cn.whale.basic.constants.MQConstants.*;

//rabbitMQ的配置
@Configuration
public class MQConfig {

    @Autowired
    private MsgConfirmCallback msgConfirmCallback;

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        rabbitTemplate.setConfirmCallback(msgConfirmCallback);
        rabbitTemplate.setReturnCallback(msgConfirmCallback);
        rabbitTemplate.setMandatory(true);
        return rabbitTemplate;
    }
}